package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.DailyPlanListAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.DailyPlanBean;
import com.yanchangapp.ui.presenter.DailyPlanAtPresenter;
import com.yanchangapp.ui.view.DailyPlanAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.functions.Consumer;

/**
 * page: 今日计划
 *
 * 可执行开始工作 & 继续工作 & 查看今日计划详情....
 */
public class DailyPlanActivity extends BaseActivity<DailyPlanAtView, DailyPlanAtPresenter> implements DailyPlanAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.rvDailyPlan)
    RecyclerView rvDailyPlan;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;

    private DailyPlanListAdapter mDailyPlanListAdapter;
    private List<DailyPlanBean> mDailyPlanBeans = new ArrayList<>();
    public static final String CMD_START_WORK = "CMD_START_WORK";
    private int curPage = 1;
    /**
     * 页面跳转...
     * @param activity
     */
    public static void show(Activity activity){
        Intent intent = new Intent(activity,DailyPlanActivity.class);
        activity.startActivity(intent);
    }

    private void setLoadingState(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.GONE){
                    llEmptyMsg.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.GONE){
                    refreshLayout.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (refreshLayout.getVisibility() == View.VISIBLE){
                    refreshLayout.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_daily_plan;
    }

    @Override
    protected DailyPlanAtPresenter createPresenter() {
        return new DailyPlanAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("今日计划");
        tvDes.setText("暂无今日计划");
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager manager =  new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        rvDailyPlan.setLayoutManager(manager);
        rvDailyPlan.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this, 0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
        mDailyPlanListAdapter = new DailyPlanListAdapter(mDailyPlanBeans);
        mDailyPlanListAdapter.setToday(true);
        rvDailyPlan.setAdapter(mDailyPlanListAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryDailyPlan(curPage);
    }

    @Override
    protected void initListener() {
        mRxManager.on(CMD_START_WORK, new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean){
                    curPage = 1;
                    mPresenter.queryDailyPlan(curPage);
                }
            }
        });


        ivToolbarNavigation.setOnClickListener(v -> finish());

        // 刷新请求事件...
        btnNextRefresh.setOnClickListener(v -> initData());

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            curPage = 1;
            mPresenter.queryDailyPlan(curPage);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            curPage ++;
            mPresenter.queryDailyPlan(curPage);
        });

        // 列表item点击事件....
        mDailyPlanListAdapter.setOnItemClickListener((adapter, view, position) -> PlanDetailActivity.show(DailyPlanActivity.this,mDailyPlanBeans.get(position).getId(),true));

        mDailyPlanListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            DailyPlanBean dailyPlanBean = mDailyPlanBeans.get(position);
            if (dailyPlanBean.getScheduleStatus() == 0){
                // 开始工作点击事件
                OperateBookActivity.show(DailyPlanActivity.this,dailyPlanBean.getId());
            }else {
                // 工作进行中事件
                LoadingOperateBookActivity.show(DailyPlanActivity.this,dailyPlanBean.getId());
            }
        });
    }

    @Override
    public void queryDailyListSuccess(List<DailyPlanBean> mList) {
        setLoadingState(LoadingType.SUCCESS);
        if (curPage == 1){
            refreshLayout.finishRefresh();
            mDailyPlanBeans.clear();
            if (mList != null && mList.size() != 0){
                mDailyPlanBeans.addAll(mList);
            }else {
                setLoadingState(LoadingType.EMPTY);
            }
            mDailyPlanListAdapter.notifyDataSetChanged();
        }else {
            if (mList != null && mList.size() != 0){
                // 添加并进行刷新操作....
                refreshLayout.finishLoadMore();
                mDailyPlanBeans.addAll(mList);
                mDailyPlanListAdapter.notifyDataSetChanged();
            }else {
                refreshLayout.finishLoadMoreWithNoMoreData();
                curPage --;
            }
        }
    }

    @Override
    public void queryDailyListFailure(String err) {
        UIUtils.showToast(err);
        if (curPage == 1){
            refreshLayout.finishRefresh();
            if (mDailyPlanBeans.size() == 0){
                setLoadingState(LoadingType.ERROR);
            }
        }else {
            curPage --;
            refreshLayout.finishLoadMore();
        }
    }
}
